<?php

namespace app\mobile\controller;
use think\Db;
use think\Controller;
use think\Session;


class Comment extends Controller
{
    // 显示最受欢迎影评
    public function index()
    {
        return view('mobile@Comment/index');
    }

    public function insert($id)
    {

        // var_dump($id);die;
        $data = Db::name('h_movie')->find($id);
        // var_dump($data);die;
        return view('mobile@Comment/insert', [
            'id' => $id,
            'title' => '我是ID:'.$id.'电影的影评页面',
            'data' => $data
        ]);
    }

    public function add()
    {
        $user = Session::get();
        $uid = $user['id'];
        $s = $_POST;
        $s['uid'] = $uid;
        $s['time'] = date("Y/m/d h:i:s",time());
        $data = Db('h_comment')->insert($s);
        $res = Db('h_comment')
                ->field('score')
                ->where('mid',$s['mid'])
                ->select();
        $total = 0;
        foreach($res as $key=>$value){
          $total += array_sum($value);
        }
        $num = count($res,COUNT_RECURSIVE);
        $n = $num/2;
        $average = number_format($total/$n,2);
        $v = [
            'score' => $average
        ];
        $score = Db('h_movie')
                ->where('id',$s['mid'])
                ->update($v);
        
        return $data;
    }

}